var Schema = mongoose.Schema;

/*
 *  ===================================活动 Activity Schema定义======================
 */
var activityMongoSchema = new Schema({
    chatGroupId: String,                                                                       //活动聊天室id
    images: [
        {
            imageURL: String,
            isBackgroundImage: {type: Boolean, default: false}

        }],
    activityCategory: {type: Schema.Types.ObjectId, ref: 'masterData_Dictionaries'},  //活动类别
    activityName: String,
    activityVenue: String,
    startDateTime: {type: Date, default: Date.now},
    endDateTime: {type: Date, default: Date.now},
    activityDesc: String,
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    broadcastScope: {type: String, enum: ['community', 'district', 'city']},        //community: 本小区； districtu: 本区；city: 本市
    joinWay: {type: String, enum: ['registration', 'postInformation']},             //registration: 报名； postInformation: 发布动态
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'},                          //发起人
    dateCreated: {type: Date, default: Date.now},                                       //发起时间

    //活动动态
    activityInformation: [
        {
            //发布活动动态的图片（多张）
            photos: [{
                photoURL: String,  //照片URL
                //对图片的评论
                commentsOnPhoto: [{
                    comments: String,
                    commentedBy: {type: Schema.Types.ObjectId, ref: 'user'},  //评论人
                    commentedDate: {type: Date, default: Date.now}               //评论日期
                }]
            }],
            //发布活动动态的文字
            textDesc: String,
            //发布人
            postedBy: {type: Schema.Types.ObjectId, ref: 'user'},
            //发布日期
            postedDate: {type: Date, default: Date.now}
        }
    ]
});
mongoose.model('activity', activityMongoSchema, 'activity');

var activityChatsMongoSchema = new Schema({
    chatGroupId: String,
    from: String,
    textMessage: String,
    nickName: String,
    portrait: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('activity_chats', activityChatsMongoSchema, 'activity_chats');

//团购
var activityNeighbourSchema = new Schema({
    displayOrder: {type: Number, default: 2},                               //顺序           //3,置顶；2，未结束；1，已结束
    community: {type: Schema.Types.ObjectId, ref: 'community'},             //所属小区
    aroundCommunity: [                                                      //周边小区
        {type: Schema.Types.ObjectId, ref: 'community', index: true}
    ],
    tag: [
        {type: Schema.Types.ObjectId, ref: 'masterData_Dictionaries'}       //标签
    ],                                                                      //问题或者投票标签
    browserCount: {type: Number, default: 0},                               //浏览数量
    praiseCount: {type: Number, default: 0},                                //点赞数量
    title: String,                                                          //标题
    desc: String,                                                           //描述
    deadline: Date,                                                         //截至日期
    category: {                                                             //类别
        type: String,
        default: 'neighbourActivity'
    },
    activityType: Number,                               //活动类型 1，团购；2，普通活动
    images: [                                                               //图片
        {
            imageURL: String,
            isValid: {type: Boolean, default: true}
        }
    ],
    partake: [                                                              //参与者
        {
            user: {type: Schema.Types.ObjectId, ref: 'user'},
            community: {type: Schema.Types.ObjectId, ref: 'community'},
            number: Number,                                                 //数量
            partakeStatus: {type: Boolean, default: false},                 //是否完成
            partakeType: Number,                                               //用户类型 1，app用户；2，微信用户
            note: String,                                                   //备注
            address: String,                                                //联系地址
            name: String,                                                    //联系姓名
            phone: String,                                                  //联系电话
            payStatus: {type: Boolean, default: false},                     //支付状态
            sendStatus: {type: Boolean, default: false},                    //发货状态
            time: {type: Date, default: Date.now}                           //
        }
    ],
    partakeNum: {type: Number, default: 0},                                //参与者人数
    status: {type: Boolean, default: true},                                //状态
    replies: [{                                                            //问题的回答字段
        replyDesc: String,
        replyStatus: {type: String, enum: ['已采纳', '未采纳'], default: '未采纳'},
        images: [
            {
                imageURL: String,
                isValid: {type: Boolean, default: true}
            }],
        replyTo: {type: Schema.Types.ObjectId, ref: 'user'},
        repliedBy: {type: Schema.Types.ObjectId, ref: 'user'},
        community: {type: Schema.Types.ObjectId, ref: 'community'},
        dateReplied: {type: Date, default: Date.now}
    }],
    level: {type: Number, default: 0},                              //0,普通帖子 1,精华贴
    isValid: {type: Boolean, default: true},
    createdBy: {type: Schema.Types.ObjectId, ref: 'user'},
    key: String,                                                     //我想key
    dateCreated: {type: Date, default: Date.now},
    lastReply: {type: Date, default: Date.now},
    dateLastModified: {type: Date, default: Date.now}
});
mongoose.model('activity_neighbour', activityNeighbourSchema, 'activity_neighbour');

var activityPartakeInfoSchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    activity: {type: Schema.Types.ObjectId, ref: 'activity_neighbour'},
    phone: String,
    address: String,
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('activity_partake_info', activityPartakeInfoSchema, 'activity_partake_info');

//社区活动
var activityCommunitySchema = new Schema({

    title: String,                          //标题
    desc: String,                           //描述
    images: [                               //图片
        {
            imageURL: String
        }
    ],
    address: String,                       //活动地址
    explain: String,                       //说明
    charge: Number,                        //费用
    type: {type: Number, default: 0},     //0,普通活动 1,闪淘
    isValid: {type: Boolean, default: true},
    beginDate: {type: Date},
    endDate: {type: Date},
    shareKey:String,
    createdBy: String,                      //登录用户
    lastModified: String,                  //最后修改用户
    dateCreated: {type: Date, default: Date.now},
    dateLastModified: {type: Date, default: Date.now}

});
mongoose.model('activity_community', activityCommunitySchema, 'activity_community');

var activityCommunityPartakeInfoSchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'user'},
    activity: {type: Schema.Types.ObjectId, ref: 'activity_community'},
    phone: String,
    desc: String,
    saleNumber: Number,
    community: {type: Schema.Types.ObjectId, ref: 'community'},
    dateCreated: {type: Date, default: Date.now}
});
mongoose.model('activity_community_partake_info', activityCommunityPartakeInfoSchema, 'activity_community_partake_info');